﻿<template>
    <div class="StandStatusDetail">
        <MyDialog :model-value="true" :type="DialogType.C3" :title="title" @close="onClose">
            <DataTable ref="StandStatusDetailDataTableRef" :data="list" divHeight="calc(100vh - 380px)" :has-add="false" :has-filter="false" :has-search="false" :has-pager="false" :has-checkbox="false" v-bind="$attrs">
                <vxe-column title="提升机类型" min-width="120" show-overflow>
                    <template #default="{ row }">
                        {{ row.hoister_Name || "-" }}
                    </template>
                </vxe-column>

                <vxe-column title="提升用途" min-width="120" show-overflow>
                    <template #default="{ row }">
                        {{ row.hoisterUsage_Name || "-" }}
                    </template>
                </vxe-column>

                <vxe-column title="钢丝绳类型" min-width="120" show-overflow>
                    <template #default="{ row }">
                        {{ row.wireropeType_Name || "-" }}
                    </template>
                </vxe-column>

                <vxe-column title="绿灯" min-width="120" show-overflow>
                    <template #default="{ row }">
                        <InputNumber class="lignt-input-number" v-model="row.greenLight" :max="100" :precision="2" prepend="<" :append="appendStr" :disabled="row.hoisterUsage_IsDelete || row.wireropeType_IsDelete"></InputNumber>
                    </template>
                </vxe-column>

                <vxe-column title="蓝灯" min-width="120" show-overflow>
                    <template #default="{ row }">
                        <InputNumber v-model="row.blueLight" :min="0" :max="100" :precision="2" prepend="<" :append="appendStr" :disabled="row.hoisterUsage_IsDelete || row.wireropeType_IsDelete"></InputNumber>
                    </template>
                </vxe-column>

                <vxe-column title="黄灯" min-width="120" show-overflow>
                    <template #default="{ row }">
                        <InputNumber v-model="row.yellowLight" :min="0" :max="100" :precision="2" prepend="<" :append="appendStr" :disabled="row.hoisterUsage_IsDelete || row.wireropeType_IsDelete"></InputNumber>
                    </template>
                </vxe-column>

                <vxe-column title="橙灯" min-width="120" show-overflow>
                    <template #default="{ row }">
                        <InputNumber v-model="row.orangeLight" :min="0" :max="100" :precision="2" prepend="<" :append="appendStr" :disabled="row.hoisterUsage_IsDelete || row.wireropeType_IsDelete"></InputNumber>
                    </template>
                </vxe-column>

                <template #otherBtn>
                    <span>备注：<el-input v-model="remark" placeholder="在这里输入备注信息" style="width: 500px"></el-input></span>
                </template>

                <!-- 顶部右侧按钮 -->
                <template #rightBtn>
                    <el-button-group>
                        <el-button type="warning" @click="coverData">复制</el-button>
                    </el-button-group>
                </template>
            </DataTable>

            <template #footer>
                <el-button type="primary" :loading="loading" @click="onSave">保存</el-button>
            </template>
        </MyDialog>
    </div>
</template>

<script lang="ts" setup>
import Enums from "@/gx/Enums";
import Gx from "@/gx/Gx";
import { DialogType, TipsType } from "@/_gx/TsEnums";

/********************************************** 接收参数 **********************************************/

/********************************************** 列表相关定义 **********************************************/

//表格的数据
const list = ref([]);

//搜索条件
const filterModel = ref<any>({});

//DataTable对象，使用此对象来调用DataTable组件中抛出的属性或方法
const StandStatusDetailDataTableRef = ref(null);

//loading...
const loading = ref(false);

const title = ref();
const standStatusID = ref();
const appendStr = ref();
const remark = ref();

/********************************************** 列表方法与事件 **********************************************/

//初始化
const Init = (row) => {
    let _data = {} as any;

    title.value = `标准维护 - ${row.name}`;
    standStatusID.value = row.standStatusID;
    remark.value = row.remark;

    if (row.type != Enums.StandStatus_Type.综合) {
        appendStr.value = "%";
    }

    MergeObjs(_data, filterModel.value);

    _data._StandStatusID = row.standStatusID;

    App.CallApi(Api.StandStatusDetail.GetSearch("[x].[StandStatusDetailID] ASC"), {
        data: _data,
        success: (res: any) => {
            list.value = res;
        },
    });
};

// 数据覆盖
const coverData = () => {
    App.Confirm({
        msg: "确定要复制第一行到所有数据？",
        type: TipsType.Warning,
        onOk: () => {
            let firstItem = list.value[0];
            for (var i = 1; i < list.value.length; i++) {
                let item = list.value[i];
                item.greenLight = firstItem.greenLight;
                item.blueLight = firstItem.blueLight;
                item.yellowLight = firstItem.yellowLight;
                item.orangeLight = firstItem.orangeLight;
                item.remark = firstItem.remark;
            }
        },
    });
};

// 保存
const onSave = () => {
    for (let i = 0; i < list.value.length; i++) {
        const item = list.value[i];

        if (Gx.IsEmpty(item.greenLight) && Gx.IsEmpty(item.blueLight) && Gx.IsEmpty(item.yellowLight) && Gx.IsEmpty(item.orangeLight)) {
            continue;
        }

        if (Gx.IsEmpty(item.greenLight) || Gx.IsEmpty(item.blueLight) || Gx.IsEmpty(item.yellowLight) || Gx.IsEmpty(item.orangeLight)) {
            App.Tips({ msg: `第${i + 1}行数据应该填写完整或都不填写！` });
            return;
        }

        if (item.greenLight >= item.blueLight || item.blueLight >= item.yellowLight || item.yellowLight >= item.orangeLight) {
            App.Tips({ msg: `第${i + 1}行数据不符合填写规则，填写数字应从前往后依次增加！` });
            return;
        }
    }

    list.value.forEach((x) => {
        x.standStatus_Remark = remark.value;
    });

    App.CallApi(Api.StandStatusDetail.BatchUpdate, {
        data: list.value,
        success: () => {
            App.Tips({ msg: "保存成功！", type: TipsType.Success });
            onClose(true);
        },
    });
};

//关闭
const onClose = (isRefresh = false) => {
    emits("close", isRefresh);
};

//向上抛出的事件与方法
const emits = defineEmits(["close"]);
defineExpose({ StandStatusDetailDataTableRef, Init });
</script>
<style scoped>
.lignt-input-number {
    width: 100%;
}
:deep(.el-input-group__prepend) {
    padding: 0 10px !important;
}
:deep(.el-input-group__append) {
    padding: 0 10px !important;
}
</style>
